<?php
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class UserController extends AdminBaseController {

    //员工列表
    public function userlist(){
        $user = M("user");
        $list = $user->where('state=1')->order()->select();
        $data['count'] = count($list);
        $workgroup = new \Admin\Controller\WorkgroupController();
        $group=$workgroup->getallgroup();
        for ($i=0; $i < $data['count']; $i++) {
            $list[$i]['workid'] = $workgroup->getnamebyid($list[$i]['workid']);
        }
        $this->assign('user',$list);
        $this->assign('group',$group);
        $this->assign('data',$data);
        $this->display("./Public/AdminLTE/pages/tables/userlist.html");
    }

    //根据员工ID获取员工信息
    public function getuserbyid($id=0){
        $user = M("user");
        $data = $user->where('id='.$id.' and state=1')->find();
        $this->ajaxReturn($data);
    }

    //根据员工id获取员工姓名
    public function getusernamebyid($id=0){
        $user = M("user");
        $name = $user->where('id='.$id)->getField('name');
        return $name;
    }

    //根据组ID获取员工总人数
    public function getusersbygpid($id=0){
        $user = M("user");
        $list = $user->where('state=1 and workid='.$id)->order()->select();
        return count($list);
    }

    //根据组ID获取空闲人数
    public function getstauserbygpid($id=0){
        $user = M("user");
        $list = $user->where('state=1 and worksta=1 and workid='.$id)->order()->select();
        return count($list);
    }

    //添加员工
    public function adduser(){
        if (I('post.name')!="") {
            $data['name'] = I('post.name');
        }
        if (I('post.workid')!="") {
            $data['workid'] = I('post.workid');
        }
        $user = M("user");
        $res = $user->data($data)->add();
        if ($res) {
            $this->success('新增成功');
        }else{
            $this->error();
        }
    }

    //更新员工
    public function edituser(){
        if (I('post.id')!="") {
            $data['id'] = I('post.id');
        }
        if (I('post.name')!="") {
            $data['name'] = I('post.name');
        }
        if (I('post.workid')!="") {
            $data['workid'] = I('post.workid');
        }
        if (I('post.worksta')!="") {
            $data['worksta'] = I('post.worksta');
        }
        $user = M("user");
        $res = $user->save($data);
        if ($res) {
            $this->success('修改成功');
        }else{
            $this->error();
        }
    }

    //删除员工
    public function deluser(){
        if (I('post.id')!="") {
            $id = I('post.id');
        }
        $user = M("user");
        $res = $user->where('id='.$id)->setField('state','2');
        if ($res) {
            $this->success('删除成功');
        }else{
            $this->error();
        }
    }



}